/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditPanel.java
 *
 * Created on Dec 6, 2009, 9:55:51 PM
 */
package wekinator;

import java.awt.CardLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.DefaultComboBoxModel;
//import wekinator.LearningSystem.EvaluationState;
//import wekinator.LearningSystem.LearningAlgorithmsInitializationState;
//TODO TODO TODO: If learning algorithm changed, or re-trained, clear records of previous evaluations

/**
 *
 * @author rebecca
 */
public class EditPanel extends javax.swing.JPanel {

    protected LearningSystem learningSystem = null;
    protected int numParams = 0;
    protected LearnerEditPanel[] learnerPanels = null;
    protected PropertyChangeListener lsPropListener = new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {
            learningSystemPropertyChanged(evt);
        }
    };

    // protected allAccurac
    /** Creates new form EditPanel */
    public EditPanel() {
        initComponents();
        WekinatorInstance.getWekinatorInstance().addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(WekinatorInstance.PROP_LEARNINGSYSTEM)) {
                    setLearningSystem(WekinatorInstance.getWekinatorInstance().getLearningSystem());
                }

            }
        });
        setLearningSystem(WekinatorInstance.getWekinatorInstance().getLearningSystem());
        panelAllAccuracy.isUsed = true;
    }

    void setLearningSystem(LearningSystem ls) {
       // learnerEditPanel1 = null;
        int oldNumParams = numParams;

        if (learningSystem == ls) {
            return;
        }

        if (learningSystem != null) {
            learningSystem.removePropertyChangeListener(lsPropListener);
        }

        if (ls != null) {
            ls.addPropertyChangeListener(lsPropListener);
           numParams = ls.getNumParams();
        } else {
            numParams = 0;
            //TODO: get rid of panels here.
            panelDrilldown.removeAll();
        }

        learningSystem = ls;

        if (ls != null) {
      //  if (numParams != oldNumParams) {
            panelDrilldown.removeAll();
            learnerPanels = new LearnerEditPanel[numParams];
            CardLayout c = (CardLayout) panelDrilldown.getLayout();
            //c.addLayoutComponent(panelAllAccuracy, "all");
            panelDrilldown.add(panelAllAccuracy, "all");
           //c.addLayoutComponent(panelAllAccuracy, "all");

            //keep this part:
            comboEditSelection.removeAllItems();
            Object[] items = new Object[numParams + 1];
            items[0] = makeObj("All models");
            SimpleDataset d = learningSystem.getDataset();
            for (int i = 0; i < numParams; i++) {
                learnerPanels[i] = new LearnerEditPanel(ls, i);
                // c.addLayoutComponent(learnerPanels[i], Integer.toString(i));
                panelDrilldown.add(learnerPanels[i], Integer.toString(i));
               // c.addLayoutComponent(learnerPanels[i], Integer.toString(i));
                items[i + 1] = makeObj(d.getParameterName(i));
            }
            comboEditSelection.setModel(new DefaultComboBoxModel(items));
            comboEditSelection.setModel(new DefaultComboBoxModel(items));
            c.show(panelDrilldown, "all");
            panelDrilldown.repaint();

       // }
        setButtonsEnabled();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        comboEditSelection = new javax.swing.JComboBox();
        panelDrilldown = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        panelAllAccuracy = new wekinator.AllAccuracy();
        jLabel1 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(446, 422));

        comboEditSelection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All models", "Model for Parameter 0 - Bow position", "Model for Parameter 1 - Pow tension", "Modle for Parameter 2 - Panning" }));
        comboEditSelection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEditSelectionActionPerformed(evt);
            }
        });

        panelDrilldown.setMaximumSize(new java.awt.Dimension(800, 800));
        panelDrilldown.setLayout(new java.awt.CardLayout());

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 440, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 389, Short.MAX_VALUE)
        );

        panelDrilldown.add(jPanel1, "card3");

        panelAllAccuracy.setMinimumSize(new java.awt.Dimension(200, 200));
        panelDrilldown.add(panelAllAccuracy, "card3");

        jLabel1.setText("Choose a model to edit/evaluate:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(comboEditSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 219, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(panelDrilldown, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(comboEditSelection, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelDrilldown, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comboEditSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEditSelectionActionPerformed
        CardLayout c = (CardLayout) panelDrilldown.getLayout();
        int i = comboEditSelection.getSelectedIndex();
        if (i == 0) {
            c.show(panelDrilldown, "all");
        } else {
            c.show(panelDrilldown, Integer.toString(i-1));
        }
}//GEN-LAST:event_comboEditSelectionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboEditSelection;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private wekinator.AllAccuracy panelAllAccuracy;
    private javax.swing.JPanel panelDrilldown;
    // End of variables declaration//GEN-END:variables

    private Object makeObj(final String item) {
        return new Object() {

            @Override
            public String toString() {
                return item;
            }
        };
    }

    private void learningSystemPropertyChanged(PropertyChangeEvent evt) {
      /*  String s = evt.getPropertyName();
        if (s.equals(LearningSystem.PROP_CVRESULTS)) {
            double[] results = learningSystem.getCvResults();
            panelAllAccuracy.updateResults(results, true);
            for (int i = 0; i < numParams; i++) {
                learnerPanels[i].getAccuracyPanel().updateResults(results, true);
            }
        } else if (s.equals(LearningSystem.PROP_TRAINRESULTS)) {
            double[] results = learningSystem.getCvResults();
            panelAllAccuracy.updateResults(results, false);
            for (int i = 0; i < numParams; i++) {
                learnerPanels[i].getAccuracyPanel().updateResults(results, false);
            }
        }/* else if (s.equals(LearningSystem.PROP_EVALUATIONSTATE)) {
        EvaluationState es = learningSystem.getEvaluationState();
        if (es == EvaluationState.DONE_EVALUATING) {
        panelAllAccuracy.evaluationFinished();
        for (int i = 0; i < numParams; i++) {
        learnerPanels[i].getAccuracyPanel().evaluationFinished();
        }
        }

        panelAllAccuracy.setEvaluationEnabled(es != EvaluationState.EVALUTATING);
        for (int i = 0; i < numParams; i++) {
        learnerPanels[i].getAccuracyPanel().setEvaluationEnabled(es != EvaluationState.EVALUTATING);
        } */
        // }
        //setButtonsEnabled();
    }

    protected void setButtonsEnabled() {
        /*   LearningSystem.LearningSystemTrainingState lsts = learningSystem.getSystemTrainingState();
        LearningSystem.DatasetState dbs = learningSystem.getDatasetState();
        LearningSystem.EvaluationState es = learningSystem.getEvaluationState();
        LearningSystem.LearningAlgorithmsInitializationState lais = learningSystem.getInitializationState();
        boolean enable = false;
        if (dbs == learningSystem.datasetState.HAS_DATA 
        && es != LearningSystem.EvaluationState.EVALUTATING
        && lais == LearningAlgorithmsInitializationState.ALL_INITIALIZED
        && lsts != LearningSystem.LearningSystemTrainingState.TRAINING) {


        enable = true;


        } 
        panelAllAccuracy.setEvaluationEnabled(enable);
        for (int i = 0; i < numParams; i++) {
        learnerPanels[i].getAccuracyPanel().setEvaluationEnabled(enable);
        }
         */
    }
}
